﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Data;
using Panaglobe.Silverlight.PhotoBrowser.Server;

/// <summary>
/// This class can render a thumbnail to response.
/// </summary>
public partial class AlbumThumbnail : System.Web.UI.Page
{
    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Guid photoID = Guid.Empty;

            if (!string.IsNullOrEmpty(this.Request["ID"]))
                photoID = new Guid(this.Request["ID"]);

            if (photoID != Guid.Empty)
            {
                SqlConnection connection = WebCommon.CreateConnection();
                SqlCommand command = new SqlCommand();
                command.Connection = connection;

                command.CommandText = "SELECT Thumbnail, ThumbnailContentType FROM PhotoAlbums WHERE ID = @ID";
                command.CommandType = System.Data.CommandType.Text;
                command.Parameters.Add("@ID", System.Data.SqlDbType.UniqueIdentifier).Value = photoID;

                byte[] arrayOfImageBytes = null;
                string contentType = string.Empty;                

                using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        arrayOfImageBytes = reader["Thumbnail"] as byte[];
                        contentType = reader["ThumbnailContentType"] as string;
                        

                        this.Response.ClearHeaders();                        
                        this.Response.ContentType = contentType;                        
                        this.Response.Expires = 0;
                        this.Response.ExpiresAbsolute = DateTime.Now;
                        this.Response.Buffer = false;

                        int readBytes = 0;
                        byte[] buffer = new byte[4096];

                        MemoryStream readStream = new MemoryStream(arrayOfImageBytes);

                        while ((readBytes = readStream.Read(buffer, 0, buffer.Length)) > 0)
                            this.Response.OutputStream.Write(buffer, 0, readBytes);

                        this.Response.Flush();
                        this.Response.End();

                        readStream.Close();
                        return;
                    }
                }
            }
        }
        catch { }

        this.Response.StatusCode = 404;
        this.Response.StatusDescription = "Not found";
        this.Response.End();
    }
}
